const { task } = require('hardhat/config')

task('interact-fundme', 'interact with fundme contract')
	.addParam('addr', 'fundme contract address')
	.setAction(async (taskArgs, hre) => {
		const fundMeFactory = await hre.ethers.getContractFactory('FundMe')
		const fundMe = fundMeFactory.attach(taskArgs.addr)

		// 2.1 init 2 accounts,ethers.getSigners()可以获取到hardhat.config.js文件中配置的accounts
		const [firstAccount, secondAccount] = await ethers.getSigners()
		// 2.2 fund contract with first account
		const fundTx = await fundMe.fund({ value: ethers.parseEther('0.5') })
		await fundTx.wait()
		// 2.3 check balance of contract ,ethers.provider相当于metamask或etherscan
		const balanceOfContract = await ethers.provider.getBalance(fundMe.target)
		console.log(`Balance of contract is: ${balanceOfContract}`)
		// 2.4 fund contract with second account, 不写.connect(account)时，用的是默认账户（hardhat.config.js文件中配置的accounts[0]）
		const fundTxWithSecondAccount = await fundMe
			.connect(secondAccount)
			.fund({ value: ethers.parseEther('0.5') })
		await fundTxWithSecondAccount.wait()
		// 2.5 check balance of contract
		const balanceOfContractAfterSecondFund = await ethers.provider.getBalance(fundMe.target)
		console.log(`Balance of contract is: ${balanceOfContractAfterSecondFund}`)
		// 2.6 check mapping fundersToAmount
		const firstAccountBalanceInFundMe = await fundMe.fundersToAmount(firstAccount.address)
		const secondAccountBalanceInFundMe = await fundMe.fundersToAmount(secondAccount.address)
		console.log(
			`Balance of first account ${firstAccount.address} is ${firstAccountBalanceInFundMe}`
		)
		console.log(
			`Balance of second account ${secondAccount.address} is ${secondAccountBalanceInFundMe}`
		)
	})
